package com.example.choupaiapp.main.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.choupaiapp.R;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.activity.find.FindDetailActivity;
import com.example.choupaiapp.main.activity.find.FindVideoDetailActivity;
import com.example.choupaiapp.main.bean.FindListBean;
import com.example.choupaiapp.main.bean.VideoListBean;
import com.example.choupaiapp.main.constant.Config;
import com.example.choupaiapp.main.view.ScaleImageView;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ASUS on 2020/1/2.
 */

public class FindChildAdapter extends RecyclerView.Adapter<FindChildAdapter.VideoHolder> {

    private List<FindListBean.ListBean> listBeans = new ArrayList<>();
    private Context context;
    private String categoriesId;


    public FindChildAdapter(Context context, String categoriesId) {
//        this.listBeans.addAll(listBeans);
        this.context = context;
        this.categoriesId = categoriesId;
    }

    @NonNull
    @Override
    public VideoHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_find_child, viewGroup, false);
        return new VideoHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull VideoHolder videoHolder, int i) {
        FindListBean.ListBean listBean = listBeans.get(i);

        videoHolder.title.setText(listBean.getTitle());
        videoHolder.name.setText(listBean.getNickname());
        videoHolder.read_count.setText(listBean.getReadSum() + "阅读");

        CircleImageView head_photo = videoHolder.head_photo;
        Glide.with(context)
                .load(listBean.getAvatar() + Config.IMAGE_LIM_FIND)
                .override(56, 56)
                .dontAnimate()
                .centerCrop()
                .placeholder(R.mipmap.icon_no_head)
                .into(head_photo);

        ScaleImageView squareImageView = videoHolder.img;
        ImageView play = videoHolder.play;
        if (listBean.getType() == 1) {
            play.setVisibility(View.GONE);
            Glide.with(context)
                    .load(listBean.getImage() + Config.IMAGE_LIM_FIND)
                    .crossFade()
                    .override(320, 320)
//                    .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .centerCrop()
                    .placeholder(R.color.color_f3f3)
                    .into(squareImageView);
        } else if (listBean.getType() == 2) {
            play.setVisibility(View.VISIBLE);
            Glide.with(context)
                    .load(listBean.getContent() + "?vframe/jpg/offset/1")
                    .crossFade()
                    .override(320, 320)
                    .centerCrop()
                    .placeholder(R.color.color_f3f3)
                    .into(squareImageView);
        } else {
            play.setVisibility(View.GONE);
            Glide.with(context)
                    .load("")
                    .crossFade()
                    .override(320, 320)
                    .centerCrop()
                    .placeholder(R.color.color_f3f3)
                    .into(squareImageView);
        }

        videoHolder.item_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isDoubleClick()) {
                    return;
                }
                listBean.setReadSum(listBean.getReadSum() + 1);
                notifyItemChanged(i);
                if (listBean.getType() == 1) {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", listBean.getId() + "");
                    startActivity(FindDetailActivity.class, bundle);
                } else if (listBean.getType() == 2) {
                    Bundle bundle = new Bundle();
                    VideoListBean videoListBean = new VideoListBean();
                    videoListBean.setVideoDownloadUrl(listBean.getContent());
                    videoListBean.setCoverImgUrl(listBean.getContent() + "?vframe/jpg/offset/1");
                    videoListBean.setTitle(listBean.getTitle());
                    videoListBean.setComment_num(listBean.getCommentSum());
                    videoListBean.setPraise_num(listBean.getLikeSum());
                    videoListBean.setHeadPhoto(listBean.getAvatar());
                    videoListBean.setNickName(listBean.getNickname());
                    videoListBean.setTime(listBean.getCreateTime());
                    videoListBean.setIsLike(listBean.getIsLike());
                    videoListBean.setAnonymity(0);
                    videoListBean.setUserId(listBean.getUserId());
                    videoListBean.setContentId(listBean.getId());
                    videoListBean.setModule(2);
                    bundle.putSerializable("data", videoListBean);
                    bundle.putInt("videoId", listBean.getId());
                    bundle.putString("flag", "mineVideo");
                    bundle.putString("categoriesId", categoriesId);
                    bundle.putInt("position", i);
                    startActivity(FindVideoDetailActivity.class, bundle);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", listBean.getId() + "");
                    startActivity(FindDetailActivity.class, bundle);
                }
            }
        });

    }

    public void startActivity(Class<?> cls, @Nullable Bundle bundle) {
        Intent intent = new Intent(context, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
    }

    public void addData(List<FindListBean.ListBean> videoList) {
        int size = listBeans.size();
        listBeans.addAll(videoList);
        //使用此方法添加数据，使用notifyDataSetChanged会导致正在播放的视频中断
        notifyItemRangeChanged(size, listBeans.size());
    }

    public void setNewData(List<FindListBean.ListBean> videoList) {
        listBeans.clear();
        listBeans.addAll(videoList);
        notifyDataSetChanged();
    }

    public List<FindListBean.ListBean> getListBeans() {
        return listBeans;
    }


    @Override
    public int getItemCount() {
        return listBeans.size();
    }

    public class VideoHolder extends RecyclerView.ViewHolder {


        private TextView title;
        private TextView name;
        private TextView read_count;
        private CircleImageView head_photo;
        private ScaleImageView img;
        private ImageView play;
        private LinearLayout item_ll;

        VideoHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            name = itemView.findViewById(R.id.name);
            read_count = itemView.findViewById(R.id.read_count);
            head_photo = itemView.findViewById(R.id.head_photo);
            img = itemView.findViewById(R.id.img);
            play = itemView.findViewById(R.id.play);
            item_ll = itemView.findViewById(R.id.item_ll);
        }
    }
}
